package com.hc.controller;

import com.hc.bean.Type;
import com.hc.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @RequestMapping("/getType/{flag}")
    @ResponseBody
    public List<Type> getTypeList(@PathVariable("flag") Integer flag){
        List<Type> typeList=typeService.getTypeList();
        if(flag!=0){
            Type type=new Type();
            type.setId(0);
            type.setName("请选择类型");
            typeList.add(0,type);
        }
        return typeList;
    }
    @RequestMapping(value = "/addType")
    @ResponseBody
    public Map addType(Type type){
        Map resultMap=new HashMap();
        if(typeService.addType(type)){
            resultMap.put("success","true");
            resultMap.put("message","商品类型添加成功!");
        }else {
            resultMap.put("success","false");
            resultMap.put("message","商品类型添加失败!");
        }
        return resultMap;
    }

    @RequestMapping(value = "/updateType")
    @ResponseBody
    public Map updateType(Type type){
        Map resultMap=new HashMap();
        if(typeService.updateType(type)){
            resultMap.put("success","true");
            resultMap.put("message","商品类型修改成功!");
        }else {
            resultMap.put("success","false");
            resultMap.put("message","商品类型修改失败!");
        }
        return resultMap;
    }
}
